<script setup  name="User">
import { ref, shallowRef } from 'vue';
import { ElMessage } from 'element-plus';
import service from '@/utils/request';
const userDate = ref({
    avaterURL: '',
    name: localStorage.getItem('adminName')||'未登录',
    id: localStorage.getItem('userId')||'未登录',
    password: localStorage.getItem('adminPassword')||'未登录',
});
const isShowDialog = ref(false);// 是否显示修改密码对话框

// 密码修改相关变量
const inputOldPassword = shallowRef('');
const newPassword = shallowRef('');
const inputConfirmPassword = shallowRef('');

const errObj = ref({
    oldPasswordErr: "",
    newPasswordErr: "",
    confirmPasswordErr: "",
});

// 密码验证函数
const oldPasswordInput = (event) => {
    inputOldPassword.value = event.target.value;
    if(inputOldPassword.value === '') {
        errObj.value.oldPasswordErr = '原密码不能为空';
        return;
    }
    if(inputOldPassword.value !== userDate.value.password) {
        errObj.value.oldPasswordErr = '原密码不正确';
        return;
    }
    errObj.value.oldPasswordErr = '';
}

const newPasswordInput = () => {
    if (newPassword.value === '') {
        errObj.value.newPasswordErr = '新密码不能为空';
        return;
    }
/*     if(/^(?=.*[A-Za-z])(?=.*\d).{6,20}$/.test(newPassword.value) === false){
        errObj.value.newPasswordErr = '密码需包含字母和数字，长度6-20位';
        return;
    } */
    errObj.value.newPasswordErr = '';
    // 如果已经输入了确认密码，重新验证
    if(inputConfirmPassword.value) {
        confirmPasswordInput({ target: { value: inputConfirmPassword.value } });
    }
}

const confirmPasswordInput = (event) => {
    inputConfirmPassword.value = event.target.value;
    if(inputConfirmPassword.value === '') {
        errObj.value.confirmPasswordErr = '确认密码不能为空';
        return;
    }
    if(inputConfirmPassword.value !== newPassword.value) {
        errObj.value.confirmPasswordErr = '两次输入的密码不一致';
        return;
    }
    errObj.value.confirmPasswordErr = '';
}

// 点击修改密码对话框的保存按钮
const modifySaveClick = () => {
    // 验证所有字段
    oldPasswordInput({ target: { value: inputOldPassword.value } });
    newPasswordInput();
    confirmPasswordInput({ target: { value: inputConfirmPassword.value } });
    
    // 检查是否有错误
    if (errObj.value.oldPasswordErr || errObj.value.newPasswordErr || errObj.value.confirmPasswordErr) {
        return;
    }
    
    service({
        method: "post",
        url: "/superadmin/message/reset",
        headers: {
            Authorization: 'Bearer ' + localStorage.getItem('token'),
        },
        data: {
            oldPassword: userDate.value.password,
            newPassword: newPassword.value,
        }
    }).then((res) => {
        if (res.message === '请求正常') {
            // 重置表单
            inputOldPassword.value = '';
            newPassword.value = '';
            inputConfirmPassword.value = '';
            errObj.value = {
                oldPasswordErr: "",
                newPasswordErr: "",
                confirmPasswordErr: "",
            };
            
            isShowDialog.value = false;
            ElMessage({
                message: '密码重置成功',
                type: 'success',
            })
        } else {
            ElMessage({
                message: '密码重置失败',
                type: 'error',
            })
        }
    })
}

// 取消修改密码
const modifyCancelClick = () => {
    // 重置表单
    inputOldPassword.value = '';
    newPassword.value = '';
    inputConfirmPassword.value = '';
    errObj.value = {
        oldPasswordErr: "",
        newPasswordErr: "",
        confirmPasswordErr: "",
    };
    
    isShowDialog.value = false;
}
</script>

<template>
    <!-- 修改密码对话框 -->
    <div class="userModifyPassword">
        <el-dialog 
        :model-value="isShowDialog" 
        width="600px" 
        top="20vh"
        :show-close="false"
        :open-delay="100"
        :close-on-click-modal="false"
        :custom-class="'custom-dialog'"
        >
        <!-- 标题部分 -->
            <template #header>
                <div class="dialogTitle">
                    <div class="line"></div>
                    <div>修改密码</div>
                </div>
            </template>
            <!-- 内容部分 -->
            <template #default>

                <div class="dialogContent">
                    <div class="oldPassword">
                        <!-- 内容的标题 -->
                        <div class="title">
                            <span>原密码：</span>
                        </div>
                        <!-- 内容的输入框 -->
                        <div class="inputPart">
                            <input 
                            type="text"
                            v-model="inputOldPassword"
                            @keyup.enter="oldPasswordInput"
                            @blur="oldPasswordInput"
                            placeholder="请输入原密码"
                            class="animated-input"
                            >
                            <div class="err" v-if="errObj.oldPasswordErr">{{ errObj.oldPasswordErr }}</div>
                        </div>
                    </div>
                    <div class="newPassword">
                        <div class="title">
                            <span>新密码：</span>
                        </div>
                        <div class="inputPart">
                            <input 
                            type="text" 
                            v-model="newPassword" 
                            @keyup.enter="newPasswordInput"
                            @blur="newPasswordInput"
                            placeholder="请输入新密码"
                            class="animated-input"
                            >
                            <div class="err" v-if="errObj.newPasswordErr">{{ errObj.newPasswordErr }}</div>
                        </div>
                    </div>
                    <div class="confirmPassword">
                        <div class="title">
                            <span>确认密码：</span>
                        </div>
                        <div class="inputPart">
                            <input 
                            type="text"
                            v-model="inputConfirmPassword"
                            @keyup.enter="confirmPasswordInput"
                            @blur="confirmPasswordInput"
                            placeholder="请再次输入新密码"
                            class="animated-input"
                            >
                            <div class="err" v-if="errObj.confirmPasswordErr">{{ errObj.confirmPasswordErr }}</div>
                        </div>
                    </div>
                    <div class="footer">
                        <div class="cancel" @click="modifyCancelClick">取消</div>
                        <div class="save" @click="modifySaveClick">保存</div>
                    </div>
                </div>
            </template>
        </el-dialog>        
    </div>
    
    <!-- 个人信息展示 -->
    <div class="User">
        <div class="layout">
            <div class="header">
                <h2>个人中心</h2>
            </div>
            <div class="container">                         
                <div class="content avatar">
                    <div class="img">
                        <img src="../../assets/image/mentorUser.webp" alt="用户头像" class="avatar-image">
                    </div>
                </div>
                <div class="content">
                    <div class="label">姓名</div>
                    <div class="data">{{ userDate.name }}</div>
                </div>
                <div class="content">
                    <div class="label">工号</div>
                    <div class="data">{{ userDate.id }}</div>
                </div>
            <div class="content">
                <div class="label">密码</div>
                <div class="modify-button-container">
                    <button class="modify-button" @click="isShowDialog = true">
                        修改密码
                    </button>
                </div>
            </div>
            </div>
        </div>
    </div>
</template>

<style scoped lang="scss">
// 全局变量
$primary-color: #4c5fbc;
$success-color: #4464a2;
$border-radius: 8px;
$box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
$transition: all 0.3s ease;

.User {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #f5f7fa;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    
    .layout {
        width: 90%;
        height: 90%;
        padding: 40px;
        background-color: white;
        border-radius: 12px;
        box-shadow: $box-shadow;
        display: flex;
        flex-direction: column;
        align-items: center;
        .header {
            width: 100%;
            height: 60px;
            margin-bottom: 20px;
            h2 {
                color: #333;
                font-size: 26px;
                font-weight: 600;
                margin: 0;
            }
        }
        .container {
            flex: 1;
            width: 100%;
            display: flex;
            flex-direction: column;
            align-items: center;
            gap: 50px;
            padding-top: 50px;
            .content {
                width: 600px;
                display: flex;
                align-items: center;
                justify-content: space-between;
                padding: 0 20px;
                .label {
                    font-size: 20px;
                    font-weight: 500;
                    color: #333;
                    width: 80px;
                }
                .data {
                    width: calc(100% - 100px);
                    height: 40px;
                    padding: 0 15px;
                    border-radius: $border-radius;
                    line-height: 40px;
                    background-color: #f8f9fa;
                    color: #666;
                    font-size: 18px;
                    transition: $transition;
                    border: 1px solid transparent;
                    
                    &:hover {
                        border-color: #e0e0e0;
                        background-color: #f0f2f5;
                    }
                }
                
                .modify-button-container {
                    width: calc(100% - 100px);
                    display: flex;
                }
                
                .modify-button {
                    padding: 8px 20px;
                    background-color: $primary-color;
                    color: white;
                    border: none;
                    border-radius: $border-radius;
                    font-size: 14px;
                    font-weight: 500;
                    cursor: pointer;
                    transition: $transition;
                    position: relative;
                    overflow: hidden;
                    
                    &:hover {
                        background-color: #4c5fbc;
                        transform: translateY(-1px);
                        box-shadow: 0 4px 8px rgba(76, 95, 188, 0.3);
                    }
                    
                    &:active {
                        transform: translateY(0);
                    }
                    
                    // 交互动画效果
                    &::before {
                        content: '';
                        position: absolute;
                        top: 50%;
                        left: 50%;
                        width: 0;
                        height: 0;
                        border-radius: 50%;
                        background-color: rgba(255, 255, 255, 0.3);
                        transition: width 0.6s, height 0.6s;
                        transform: translate(-50%, -50%);
                    }
                    
                    &:active::before {
                        width: 300px;
                        height: 300px;
                    }
                }
            }
            
            .avatar {
                flex-direction: column;
                gap: 15px;
                text-align: center;
                
                .label {
                    width: auto;
                }
                
                .img {
                    width: 100px;
                    height: 100px;
                    border-radius: 50%;
                    overflow: hidden;
                    box-shadow: $box-shadow;
                    .avatar-image {
                        width: 100%;
                        height: 100%;
                        object-fit: cover;
                        transition: $transition;
                        
                        &:hover {
                            transform: scale(1.05);
                        }
                    }
                }
            }
        }
    }
}

// 修改密码对话框样式
:deep(.custom-dialog) {
    // 对话框动画
    animation: dialogFadeIn 0.4s cubic-bezier(0.25, 0.8, 0.25, 1);
    
    // 自定义关闭按钮
    .el-dialog__headerbtn {
        top: 15px;
        right: 15px;
        font-size: 18px;
        
        &:hover {
            color: $primary-color;
            background-color: rgba(76, 95, 188, 0.1);
            border-radius: 50%;
        }
    }
    
    // 对话框内容区域
    .el-dialog__body {
        padding: 0 !important;
    }
}

@keyframes dialogFadeIn {
    from {
        opacity: 0;
        transform: scale(0.9) translateY(-20px);
    }
    to {
        opacity: 1;
        transform: scale(1) translateY(0);
    }
}

.dialogTitle {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 20px;
    font-weight: 600;
    color: #333;
    padding: 10px 0;
    
    .line {
        width: 4px;
        height: 24px;
        border-radius: 2px;
        background-color: $primary-color;
        animation: lineGrow 0.5s ease-out;
    }
}

@keyframes lineGrow {
    from {
        height: 0;
    }
    to {
        height: 24px;
    }
}

.dialogContent {
    width: 100%;
    padding: 20px 30px 30px;
    display: flex;
    flex-direction: column;
    gap: 24px;
    font-weight: 500;
    
    .oldPassword, .newPassword, .confirmPassword {
        width: 100%;
        display: flex;
        align-items: flex-start;
        gap: 15px;
        position: relative;
        
        .title {
            width: 100px;
            font-size: 16px;
            color: #333;
            padding-top: 10px;
            flex-shrink: 0;
            
            &::before {
                content: '*';
                color: #ff4d4f;
                margin-right: 4px;
            }
        }
        
        .inputPart {
            position: relative;
            flex: 1;
            
            .animated-input {
                width: 100%;
                height: 42px;
                border: 1px solid #d9d9d9;
                border-radius: $border-radius;
                background-color: #fff;
                text-indent: 12px;
                font-size: 14px;
                transition: $transition;
                position: relative;
                z-index: 1;
                
                &:focus {
                    outline: none;
                    border-color: $primary-color;
                    box-shadow: 0 0 0 3px rgba(76, 95, 188, 0.1);
                    // 聚焦时的动画效果
                    &::after {
                        content: '';
                        position: absolute;
                        top: -1px;
                        left: -1px;
                        right: -1px;
                        bottom: -1px;
                        border-radius: $border-radius;
                        border: 2px solid transparent;
                        background: linear-gradient(90deg, $primary-color, #6c85e9) border-box;
                        -webkit-mask: linear-gradient(#fff 0 0) padding-box, linear-gradient(#fff 0 0);
                        -webkit-mask-composite: destination-out;
                        mask-composite: exclude;
                        z-index: -1;
                        animation: borderPulse 1.5s infinite;
                    }
                }
                
                &::placeholder {
                    color: #bfbfbf;
                    transition: color 0.3s ease;
                }
                
                &:focus::placeholder {
                    color: #a0a0a0;
                }
            }
            
            .err {
                position: absolute;
                top: 100%;
                left: 0;
                font-size: 12px;
                color: #ff4d4f;
                margin-top: 6px;
                animation: shake 0.3s ease;
            }
        }
    }
    
    .footer {
        width: 100%;
        margin-top: 20px;
        display: flex;
        align-items: center;
        justify-content: flex-end;
        gap: 15px;
        font-weight: 400;
        
        .save, .cancel {
            min-width: 80px;
            height: 38px;
            border-radius: $border-radius;
            text-align: center;
            line-height: 38px;
            font-size: 14px;
            cursor: pointer;
            transition: $transition;
            font-weight: 500;
            position: relative;
            overflow: hidden;
        }
        
        .save {
            background-color: $success-color;
            color: white;
            
            &:hover {
                background-color: #3a558a;
                transform: translateY(-1px);
                box-shadow: 0 4px 12px rgba(68, 100, 162, 0.3);
            }
            
            &:active {
                transform: translateY(0);
            }
            
            // 点击波纹效果
            &::before {
                content: '';
                position: absolute;
                top: 50%;
                left: 50%;
                width: 0;
                height: 0;
                border-radius: 50%;
                background-color: rgba(255, 255, 255, 0.4);
                transition: width 0.6s, height 0.6s;
                transform: translate(-50%, -50%);
            }
            
            &:active::before {
                width: 200px;
                height: 200px;
            }
        }
        
        .cancel {
            background-color: #f5f5f5;
            color: #666;
            
            &:hover {
                background-color: #e6e6e6;
                color: #333;
                transform: translateY(-1px);
            }
            
            &:active {
                transform: translateY(0);
            }
        }
    }
}

@keyframes shake {
    0%, 100% { transform: translateX(0); }
    20%, 60% { transform: translateX(-3px); }
    40%, 80% { transform: translateX(3px); }
}

@keyframes borderPulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.7; }
}

// 输入框填充时的效果
.oldPassword, .newPassword, .confirmPassword {
    &:has(.animated-input:not(:placeholder-shown)) .title {
        color: $primary-color;
        transition: color 0.3s ease;
    }
}

// 响应式设计
@media (max-width: 500px) {
    :deep(.custom-dialog) {
        width: 90% !important;
        margin: 0 auto;
    }
    
    .dialogContent {
        padding: 15px 20px 25px;
        
        .oldPassword, .newPassword, .confirmPassword {
            flex-direction: column;
            gap: 8px;
            
            .title {
                width: 100%;
                padding-top: 0;
            }
        }
        
        .footer {
            flex-direction: column-reverse;
            gap: 10px;
            
            .save, .cancel {
                width: 100%;
            }
        }
    }
}
</style>